<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BOnlineAppointment
 *
 * @author I062261
 */
class BBookingSports extends BApp {

    //put your code here

    public function getResourceList($data = NULL) {
        //$user = Blyn::app()->getCurrentUser();
        $groupContext = NULL;
        $resourceContext = new BSResource();
        $resourceContext->creatorId = Blyn::app()->getCurrentUser()->getId();
        $resourceContext->spaceId = Blyn::app()->getActiveSpace()->getId();
        $resourceContext->appId = Blyn::app()->getActiveApp()->getId();

        $list = array();

        if (isset($data['groupContext']))
            $groupContext = $data['groupContext'];
        if (isset($data['resourceContext']))
            $resourceContext = $data['resourceContext'];

        if ($groupContext != NULL) {
            $rGroup = BSReourceGroup::find($groupContext);

            if ($rGroup instanceof BGroup) {
                $list = $rGroup->getMembers($resourceContext);
                //return $list;
            }
        } else {
            $list = BSResource::findAll($resourceContext);
            //return $list;
        }

        foreach ($list as $id => $resource) {
            if (!BPermit::checkUserPermit($resource, BPermit::AllowModify))
                unset($list[$id]);
        }

        if (is_array($data) && isset($data['format'])) {
            if ($data['format'] == 'stringList') {
                $sList = array();
                foreach ($list as $key => $r) {
                    $sList[$key] = $r->getName();
                }
                return $sList;
            }
        }

        return $list;
    }
    
    public function getResourceGroupListData($id) {
        return BSResourceGroup::findByConstantId($id)->getTreeListData();
    }

    public function getTimeslots($format = 'stringList') {
        $timeslots = BSTimeslot::findAll();
        if ($format === 'stringList') {
            $timeslotList = array();
            foreach ($timeslots as $id => $item) {
                $timeslotList[$id] = $item->getName();
            }
            return $timeslotList;
        }
        return $timeslots;
    }

    public function getBookableResourceList($context = NULL, $groupContext = NULL, $data = NULL) {
        //$user = Blyn::app()->getCurrentUser();
        $resourceContext = $context;
        if ($context == NULL) {
            $resourceContext = new BSBookableResource();
            //$resourceContext->ownerId = Blyn::app()->getCurrentUser()->getId();
            $resourceContext->spaceId = Blyn::app()->getActiveSpace()->getId();
            $resourceContext->appId = Blyn::app()->getActiveApp()->getId();
        }

        $list = array();

        if ($groupContext != NULL) {
            $rGroup = BSBookableResourceGroup::find($groupContext);
            if ($rGroup instanceof BGroup) {
                $list = $rGroup->getMembers($resourceContext);
                //return $list;
            }
        } else {
            $list = BSBookableResource::findAll($resourceContext);
            //return $list;
        }

        foreach ($list as $id => $resource) {
            if (!BPermit::checkUserPermit($resource, BPermit::AllowModify))
                unset($list[$id]);
        }

        if (is_array($data) && isset($data['format'])) {
            if ($data['format'] == 'stringList') {
                $sList = array();
                foreach ($list as $key => $r) {
                    $sList[$key] = $r->getName();
                }
                return $sList;
            }
        }

        return $list;
    }

    public function getBookableResourceTimeSlotTypeList($data = 'stringList') {
        $resourcTypes = BSTimeslotGroup::find('timeslotType')->getChildren();

        if (is_array($data) && isset($data['format'])) {
            if ($data['format'] == 'stringList') {
                $sList = array();
                foreach ($resourcTypes as $key => $r) {
                    $sList[$key] = $r->getName();
                }
                return $sList;
            }
        }

        return $resourcTypes;
    }

    //this function will insert or update resource
    public function addResource($resourceModel = NULL) {

        $creatorId = Blyn::app()->getCurrentUser()->getId();
        if ($resourceModel instanceof BSResourceFormModel) {
            if ($resourceModel->id == 0 || $resourceModel->id == NULL) {
                $resource = new BSResource();
                $resource->name = $resourceModel->name;
                $resource->description = $resourceModel->description;
                $resource->amount = $resourceModel->amount;
                $resource->save();
                BPermit::addPermit($resource, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

                $this->saveResourceGroupMemebers($resourceModel, $resource);

                return $resource->id;
            } else if ($resourceModel->id > 0) {
                $resource = new BSResource($resourceModel->id);
                if ($resourceModel->name != NULL)
                    $resource->name = $resourceModel->name;
                if ($resourceModel->amount != NULL)
                    $resource->amount = $resourceModel->amount;
                if ($resourceModel->description != NULL)
                    $resource->description = $resourceModel->description;

                $resource->save();
                BPermit::addPermit($resource, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

                BSResourceGroup::removeMembersByMemberId($resource->id);
                $this->saveResourceGroupMemebers($resourceModel, $resource);
            }
        }
    }

    function saveResourceGroupMemebers($resourceModel, $resource) {
        $resourceGroup = new BSResourceGroup($resourceModel->rgSportType);
        $resourceGroup->addMember($resource);

        $resourceGroup = new BSResourceGroup($resourceModel->rgArea);
        $resourceGroup->addMember($resource);

        $resourceGroup = new BSResourceGroup($resourceModel->rgStadium);
        $resourceGroup->addMember($resource);
    }

    //this function will insert or update resource
    public function addBookableResource($bookableModel = NULL, $timeslotModel = NULL) {
        $creatorId = Blyn::app()->getCurrentUser()->getId();
        if ($bookableModel instanceof BSBookableResourceFormModel) {
            if ($bookableModel->id == 0 || $bookableModel->id == NULL) {

                if ($bookableModel->timeslotId > 0) {
                    $timeslotId = $bookableModel->timeslotId;
                } else {
                    $timeslotId = $this->addTimeslotResource($timeslotModel);
                }

                $resource = new BSBookableResource();
                $resource->timeslotId = $timeslotId;
                $resource->resourceId = $bookableModel->resourceId;
                $resource->amount = $bookableModel->amount;
                $resource->unitPrice = $bookableModel->unitPrice;
                $resource->repeatStartTime = $bookableModel->repeatStartTime;
                $resource->repeatEndTime = $bookableModel->repeatEndTime;
//                $resource->amount = $resourceModel->amount;
                $resource->save();
                BPermit::addPermit($resource, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

                return $resource->id;
            } else if ($bookableModel->id > 0) {
                $resource = new BSBookableResource($bookableModel->id);

                if ($bookableModel->timeslotId > 0) {
                    $timeslotId = $bookableModel->timeslotId;
                } else {
                    $timeslotId = $this->addTimeslotResource($timeslotModel);
                }

                $resource->timeslotId = $timeslotId;

                if ($bookableModel->amount != NULL)
                    $resource->amount = $bookableModel->amount;
                if ($bookableModel->resourceId != NULL)
                    $resource->resourceId = $bookableModel->resourceId;
                if ($bookableModel->unitPrice != NULL)
                    $resource->unitPrice = $bookableModel->unitPrice;
                if ($bookableModel->repeatStartTime != NULL)
                    $resource->repeatStartTime = $bookableModel->repeatStartTime;
                if ($bookableModel->repeatEndTime != NULL)
                    $resource->repeatEndTime = $bookableModel->repeatEndTime;

                $resource->save();
                BPermit::addPermit($resource, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));
            }
        }
    }

    public function addTimeslotResource($timeslotModel = NULL) {
        if ($timeslotModel instanceof BSTimeslotFormModel) {
            if ($timeslotModel->id == 0 || $timeslotModel->id == NULL) {
                $timeslot = new BSTimeslot();
                $timeslot->name = $timeslotModel->name;
                $timeslot->cronValue = $timeslotModel->cronValue;
                $timeslot->description = $timeslotModel->description;
                $timeslot->save();
//                BPermit::addPermit($timeslot, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));

                return $timeslot->id;
            } else if ($timeslotModel->id > 0) {
                $timeslot = new BSTimeslot($timeslotModel->id);
                if ($timeslotModel->name != NULL)
                    $timeslot->name = $timeslotModel->name;
                if ($timeslotModel->cronValue != NULL)
                    $timeslot->cronValue = $timeslotModel->cronValue;
                if ($timeslotModel->description != NULL)
                    $timeslot->description = $timeslotModel->description;

                $timeslot->save();
//                BPermit::addPermit($resource, BPermit::AllowModify, Blyn::app()->getActiveApp()->getRole(BRole::Role_Space_Admin), array('userId' => $creatorId));
            }
        }
    }

    public function deleteResource($resourceModel) {

        if ($resourceModel instanceof BSResourceFormModel) {
            if ($resourceModel->id > 0) {
                BSResource::delete(array('id' => $resourceModel->id));
                return 'deleted resource ' . $resourceModel->id;
            }
        }

        return FALSE;
    }

    public function deleteBookableResource($resourceModel) {

        if ($resourceModel instanceof BSBookableResourceFormModel) {
            if ($resourceModel->id > 0) {
                BSBookableResource::delete(array('id' => $resourceModel->id));
                return 'deleted bookable resource ' . $resourceModel->id;
            }
        }

        return FALSE;
    }

    public function deleteTimeslot($timeslotModel) {

        if ($timeslotModel instanceof BSTimeslotFormModel) {
            if ($timeslotModel->id > 0) {
                BSTimeslot::delete(array('id' => $timeslotModel->id));
                return 'deleted timeslot ' . $timeslotModel->id;
            }
        }

        return FALSE;
    }

    public function checkOwnerPermit($resource) {

        if ($resource instanceof BEntity) {
            if (property_exists(get_class($resource), 'ownerId')) {
                $ownerId = $resource->ownerId;
                if ($ownerId != NULL && $ownerId > 0) {
                    $owner = new BSOwner($ownerId);
                    if ($owner->spaceId != NULL) {
                        if ($owner->spaceId != Blyn::app()->getActiveSpace()->getId())
                            return FALSE;
                    }
                    if ($owner->appId != NULL) {
                        if ($owner->appId != Blyn::app()->getActiveApp()->getId())
                            return FALSE;
                    }
                    if ($owner->circleId != NULL) {
                        return FALSE;
                    }
                    if ($owner->userId != NULL) {
                        if ($owner->userId != Blyn::app()->getCurrentUser()->getId())
                            return FALSE;
                    }
                }
            }
        }

        return TRUE;
    }

    //this function add or update timeslot
    public function addTimeSlot($data = NULL) {
        
    }

    /*
      public function getUIManager($data = NULL) {
      parent::getUIManager($data);
      if ($this->uiManager instanceof BUIBookSports)
      $this->uiManager = new BUIBookSports($this);

      return $this->uiManager;
      }
     * 
     */

    /**
     * this function will be call before render,
     * can add 
     * @param type $space is the active space
     */
    public function runSpace($space, $data = NULL) {
        
    }

    /**
     * this function like Yii action, $operation is the operation who will be run
     * before render, you can find operation from configure file, for each operation
     * defined in application config file, can always 
     * @param type $operation, active operation 
     * @param type $data
     */
    public function runBookingFacility($operation, $data = NULL) {
        
    }

    public function runManageResource($operation, $data = NULL) {
        
    }

    public function runManageBookableItem($operation, $data = NULL) {
        
    }

}

?>
